package com.yanqu.road.server.cmd.chefduel.process3;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.chefduel.process3.ChefDuel3OpenItem;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.chefduel.ChefDuelModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelActivityMgr;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_CHEF_DUEL3_OPEN_BANQUET, desc = "开桌子")
public class ChefDuel3OpenBanquetCmd extends AbstractCommand {

	private int activityId;
	private int itemId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		ChefDuelProto.ChefDuel3OpenBanquetReqMsg reqMsg = ChefDuelProto.ChefDuel3OpenBanquetReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		itemId = reqMsg.getItemId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = ChefDuelActivityMgr.getInTimeActivityInfo(activityId);
		if(activityInfo == null){
			ret = GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME;
			return;
		}
		if(!ChefDuelActivityMgr.isActivityMatchTime(activityInfo)){
			ret = GameErrorCode.E_CHEF_DUEL_NOT_IN_MATCH_TIME;
			return;
		}
		if(!player.getModule(ChefDuelModule.class).isAble(activityId)){
			ret = GameErrorCode.E_CHEF_DUEL2_USER_NOT_ABLE;
			return;
		}
		ChefDuel3OpenItem cookBoy3OpenItem = ChefDuelActivityMgr.getChefDuel3OpenItem(activityId,itemId);
		if(cookBoy3OpenItem == null){
			ret = GameErrorCode.E_CHEF_DUEL3_OPEN_ITEM_NOT_FOUND;
			return;
		}
		if(player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(cookBoy3OpenItem.getConsume()))){
			player.getModule(PreReduceModule.class).preReduce(cookBoy3OpenItem.getConsume());
		}else{
			ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
			return;
		}
		ret = ChefDuelMgr.sendOpenTable(activityId,player.getUserId(),itemId);
		ChefDuelMgr.addSceneUser(activityId,player.getUserId(),player.getUserId());
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			ChefDuelProto.ChefDuel3OpenBanquetRespMsg.Builder respMsg = ChefDuelProto.ChefDuel3OpenBanquetRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_CHEF_DUEL3_OPEN_BANQUET, respMsg);
		}
	}
}
